package com.example.customutils;

public class StringUtils {

	// ///////////////////////////////////////////////////////////////////////
	// Counting Methods //
	// ///////////////////////////////////////////////////////////////////////

	/**
	 * Count the number of instances of substring within a string.
	 * 
	 * @param string
	 *            String to look for substring in.
	 * @param substring
	 *            Sub-string to look for.
	 * @return Count of substrings in string.
	 */
	public static int count(final String string, final String substring) {
		int count = 0;
		int idx = 0;

		while ((idx = string.indexOf(substring, idx)) != -1) {
			idx++;
			count++;
		}

		return count;
	}
	
	public static int countAnotherWay(final String string, final String substring) {
		return string.split(substring).length - 1;
	}

	/**
	 * Count the number of instances of character within a string.
	 * 
	 * @param string
	 *            String to look for substring in.
	 * @param c
	 *            Character to look for.
	 * @return Count of substrings in string.
	 */
	public static int count(final String string, final char c) {
		return count(string, String.valueOf(c));
	}
}
